/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package BeanView;

import Dao.DaoMiddleName;
import HibernateUtil.HibernateUtil;
import Pojo.Employees;
import Pojo.Middlename;
import java.util.Arrays;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author olegk
 */
@ManagedBean
@ViewScoped
public class MbVMiddleName implements java.io.Serializable {
    private Session session;
    private Transaction transaction;
    private Middlename selectedMiddleName;
    private Middlename middleName;
    private List<Middlename> listMiddleName;
    private List<Middlename> listMiddleNameFiltered;
    private String newName;
    
    public MbVMiddleName(){
        this.middleName=new Middlename();
        this.middleName.setId(0L);
        this.newName = "";
    }
    
    public void registration(){
        this.session=null;
        this.transaction=null;
        
        try
        {       
            if(this.newName.trim().equals(""))
            {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Ошибка:", "Укажите отчество"));
                return;
            }
            DaoMiddleName daoMiddleName=new DaoMiddleName();            
            this.session=HibernateUtil.getSessionFactory().openSession();
            this.transaction=session.beginTransaction();
            String query = "from Middlename m where lower(m.name)=lower(?)";
            List<Object> param = Arrays.asList((Object)this.newName);
            if(daoMiddleName.selectMiddleName(this.session, query, param)!=null)
            {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Ошибка:", "Отчество уже зарегистрировано в системе"));
                return;
            }
            this.middleName.setName(this.newName);             
            daoMiddleName.register(this.session, this.middleName);            
            this.transaction.commit();            
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Успех:", "Регистрация прошла успешно"));
            this.middleName=new Middlename();
            this.middleName.setId(0L);
        }
        catch(Exception ex)
        {
            if(this.transaction!=null)
            {
                this.transaction.rollback();
            }            
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL, "Фатальная ошибка:", "Пожалуйста, обратитесь к администратору "+ex.getMessage()));
        }
        finally
        {
            if(this.session!=null)
            {
                this.session.close();
            }
        }
    }
    
    public List<Middlename> getAll(){
        this.session=null;
        this.transaction=null;
        
        try
        {
            DaoMiddleName daoMiddleName=new DaoMiddleName();            
            this.session=HibernateUtil.getSessionFactory().openSession();
            this.transaction=this.session.beginTransaction();            
            this.listMiddleName=daoMiddleName.getAll(this.session);            
            this.transaction.commit();            
            return this.listMiddleName;
        }
        catch(Exception ex)
        {
            if(this.transaction!=null)
            {
                this.transaction.rollback();
            }            
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL, "Фатальная ошибка:", "Пожалуйста, обратитесь к администратору "+ex.getMessage()));            
            return null;
        }
        finally
        {
            if(this.session!=null)
            {
                this.session.close();
            }
        }        
    }
    
    public Middlename getId(String id){
        this.session=null;
        this.transaction=null;        
        try
        {
            DaoMiddleName daoMiddleName=new DaoMiddleName();             
            this.session=HibernateUtil.getSessionFactory().openSession();
            this.transaction=this.session.beginTransaction();  
            String query = "from Middlename mn where mn.id = ?";
            List<Object> param = Arrays.asList((Object)Long.parseLong(id));
            this.middleName=daoMiddleName.selectMiddleName(this.session, query, param);           
            this.transaction.commit();            
            return this.middleName;
        }
        catch(Exception ex)
        {
            if(this.transaction!=null)
            {
                this.transaction.rollback();
            }            
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL, "Фатальная ошибка:", "Пожалуйста, обратитесь к администратору "+ex.getMessage()));            
            return null;
        }
        finally
        {
            if(this.session!=null)
            {
                this.session.close();
            }
        }
    }

    public void remove(){
        this.session=null;
        this.transaction=null;        
        try
        {            
            DaoMiddleName daoMiddleName=new DaoMiddleName();
            this.session=HibernateUtil.getSessionFactory().openSession();
            this.transaction=session.beginTransaction();                     
            daoMiddleName.delete(this.session, this.middleName);            
            this.transaction.commit();            
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Успех:", "Фамилия удалена"));
            this.middleName=new Middlename();
            this.middleName.setId(0L);
        }
        catch(Exception ex)
        {
            if(this.transaction!=null)
            {
                this.transaction.rollback();
            }            
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL, "Фатальная ошибка:", "Пожалуйста, обратитесь к администратору "+ex.getMessage()));
        }
        finally
        {
            if(this.session!=null)
            {
                this.session.close();
            }
        }
    }

    public void update(){
        this.session=null;
        this.transaction=null;        
        try
        {            
            DaoMiddleName daoMiddleName=new DaoMiddleName();
            this.session=HibernateUtil.getSessionFactory().openSession();
            this.transaction=session.beginTransaction();
            String query = "from Middlename m where m.id <> ? and m.name = ?";
            List<Object> param = Arrays.asList((Object)this.middleName.getId(), 
                                (Object)this.middleName.getName());
            if(daoMiddleName.selectMiddleName(this.session, query, param)!=null)
            {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Ошибка:", "Организация уже зарегистрирована в системе"));
                return;
            }           
            daoMiddleName.update(this.session, this.middleName);            
            this.transaction.commit();            
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Успех:", "Регистрация прошла успешно"));
            this.middleName=new Middlename();
            this.middleName.setId(0L);
        }
        catch(Exception ex)
        {
            if(this.transaction!=null)
            {
                this.transaction.rollback();
            }            
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL, "Фатальная ошибка:", "Пожалуйста, обратитесь к администратору "+ex.getMessage()));
        }
        finally
        {
            if(this.session!=null)
            {
                this.session.close();
            }
        }
    }

    public void setMiddleNameParent(){
        Object parent=FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("parent");
        if(parent instanceof Employees){
            Employees employees=(Employees)parent;
            employees.setMiddlename(this.middleName);
        } 
        this.middleName=new Middlename();
        this.middleName.setId(0L);
    }

    public String cancel(){
        this.middleName=new Middlename();
        this.middleName.setId(0L);
        return null;
    }
    
    public Middlename getSelectedMiddleName() {
        return selectedMiddleName;
    }

    public void setSelectedMiddleName(Middlename selectedMiddleName) {
        this.selectedMiddleName = selectedMiddleName;
    }

    public Middlename getMiddleName() {
        return middleName;
    }

    public void setMiddleName(Middlename middleName) {
        this.middleName = middleName;
    }

    public List<Middlename> getListMiddleName() {
        return listMiddleName;
    }

    public void setListMiddleName(List<Middlename> listMiddleName) {
        this.listMiddleName = listMiddleName;
    }

    public List<Middlename> getListMiddleNameFiltered() {
        return listMiddleNameFiltered;
    }

    public void setListMiddleNameFiltered(List<Middlename> listMiddleNameFiltered) {
        this.listMiddleNameFiltered = listMiddleNameFiltered;
    }

    public String getNewName() {
        return newName;
    }

    public void setNewName(String newName) {
        this.newName = newName;
    }    
}
